/***************************************************
> Copyright (C) 2024 ==RXX== All rights reserved.
> File Name: 1.c
> Author: rxx
> Mail:2909017383@qq.com 
> Created Time: 2024年12月3日 星期二 11时27分08秒
***************************************************/
#include "userxx.h"
//菜单
void menu1(STU *head)
{
	int flag1 = 1;
	char value[20];	
	int num1 = 0;
	//申请头节点
	head = inputhead(head);
	while(flag1)
	{

		printf("########################################################\n");
		printf("\t\t\t1 注册新用户\n");
		printf("\t\t\t2 登陆\n");
		printf("\t\t\t3 修改密码\n");
		printf("\t\t\t4 退出\n");
		printf("########################################################\n");
		printf("输入要执行的内容编号：\n");
		scanf("%d",&num1);
		switch(num1)
		{
			case 1:
				//注册
				head = newuser(head);//注册
				break;
			case 2:
			    //登陆
				if(head->next == NULL)
				{
					printf("没有用户请注册!\n");
				}else
				{
					logOn(head);
				}
				break;
			case 3:
			    //修改密码
				printf("输入要修改的用户：\n");
				scanf("%s",value);
				modify(head,value);	
				break;
			default:
				//打印注册的用户及其密码
				output(head);
				flag1 = 0;
				break;
		}
	}
}
//创建头节点
STU *inputhead(STU *head)
{
	head = (STU*)malloc(sizeof(STU));
	memset(head,0,sizeof(STU));
	if(head == NULL)
	{
		perror("malloc");
		exit(1);
	}
	return head;
}
//注册新用户
STU *newuser(STU *head)
{
	assert(head != NULL);
	STU *end = head;//尾节点
	while(end->next != NULL)
	{
		end = end->next; 
	}
	//给新节点申请空间
	STU *node = (STU*)malloc(sizeof(STU));
	memset(node,0,sizeof(STU));

	//赋值
	printf("输入newuser：");
	scanf("%s",node->user);
	printf("输入newpassword：");
	scanf("%s",node->password);
	
	if(strlen(node->user) >= 6 && strlen(node->user) <= 8)
	{
		if(strlen(node->password) >= 6 && strlen(node->password) <= 8)
		{
			end->next = node;
			node->next = NULL;
			printf("注册成功。\n");
		}
	}else
	{
		printf("密码或用户输入格式不正确！！！\n");
		printf(" 注册失败请重新输入！！!\n");
	}

	return head;
}
//输出链表
STU *output(STU *head)
{
	STU *end = head->next;//尾节点
	while(end != NULL)
	{
		printf("user:%s\n",end->user);
		printf("password:%s\n",end->password);
		end = end->next;
	}
	return head;
}
//登陆用户
int logOn(STU *head)
{
	STU *end = head->next;//尾节点
	char user[20];
	char password[20];
	int isOk = 0;
	printf("输入user：");
	scanf("%s",user);//输入用户和密码
	printf("输入password：");
	scanf("%s",password);
	while(end != NULL)
	{
	   	if( strcmp(end->user,user) == 0 && 
		strcmp(end->password,password) == 0 )
		{
			printf("登陆成功！\n");
			isOk = 1;//对比成功
			reservation(isOk,head);
			break;
		}
		end = end->next;
	}
	if(end == NULL)
	{
		printf("登陆失败！\n");
		isOk = 0;
	}//对比不成功
	return isOk;
}
//预约洗衣机
void reservation (int isOk,STU *head)
{
	int flag2 = 1;
	int flag3 = 1;
	int num2 = 0;
	int washing[5] = {0 , 1, 0 , 1, 0};
	while(flag2)
	{
		printf("########################################################\n");
		printf("\t\t\t1号洗衣机\n");
		printf("\t\t\t2号洗衣机\n");
		printf("\t\t\t3号洗衣机\n");
		printf("\t\t\t4号洗衣机\n");
		printf("\t\t\t5号洗衣机\n");
		printf("########################################################\n");
		printf("请选择要预约的洗衣机：");
		printf("请输入要预约的机器：");
		scanf("%d",&num2);
		while(flag3)
		{
			if(washing[num2-1] == 0)
			{
				washing[num2-1] == 1;
				printf("预约成功！\n");
				flag3 = 0;
			}
			else if(washing[num2-1] == 1)
			{
				printf("已经被占用请重新选择！\n");
				flag3 = 0;
			}
			else if(washing[num2-1] == -1) 
			{
				printf("已故障请重新选择！\n");
				flag3 = 0;
			}
			else
			{
				printf("输入错误，请重新预约！\n");
				flag3 = 0;
			}
		}
		flag2 = 0;
	}
}

//修改节点的值
STU *modify(STU *head,char *value)
{
	STU *end = head->next;//尾节点
	char str[20] ;
	while(end != NULL)
	{
		if( (strcmp(end->user ,value)) == 0)
		{
			printf("输入要修改的密码：");
			scanf("%s",str);
			strcpy(end->password,str);
			printf("修改成功！\n");
			return 0;
		}
		end = end->next;
	}
	if(end == NULL)
	{
		printf("没有该用户！\n");
	}
	return head;
}
//释放空间
void freeSpace(STU *head)
{
	STU *tmp = head;//令tmp = head
	while(tmp != NULL)//tmp不等于0的时候进入循环
	{
		head = tmp;//让head等于tmp
		tmp = tmp->next;//让tmp等于tmp的下一个
		free(head);//释放head
	}
}
